package me.rvj.servlet;

import me.rvj.dao.UserDao;
import me.rvj.entity.ServerSocket;
import me.rvj.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

/**
 * @program: JavaWeb
 * @description:
 * @author: Rv_Jiang
 * @date: 2021/7/2 9:45
 */
@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String userName = req.getParameter("userName").trim();
        String password = req.getParameter("password").trim();

        List<User> userList = UserDao.getUserList();

        for (User user : userList) {
            if (user.getUserName().equals(userName)) {
                if (user.getPassword().equals(password)) {

                    resp.getWriter().print(true);

//                    OnlineUserList添加
                    ServerSocket.onlineUserList.put(userName,user);

                    return;

                } else {
                    break;
                }
            }
        }
        resp.getWriter().print(false);
//        resp.sendError(404,"账号密码错误");
    }

}
